using Microsoft.AspNetCore.Mvc;
using StudyCompanion.Commons.Result;
using StudyCompanion.Core.API;
using StudyCompanion.Core.Share;
using StudyCompanion.Dto.Study;
using StudyCompanion.IServices;
using StudyCompanion.Model.Study;

namespace StudyCompanion.WebApi.Controllers;

/// <summary>
///     学习目录
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class StudyController : ApiController
{
    private readonly IUserContext _userContext;

    private readonly IStudyService _studyService;

    public StudyController(IStudyService studyService, IUserContext userContext)
    {
        _studyService = studyService;
        _userContext = userContext;
    }

    /// <summary>
    ///     年级列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("ClassList")]
    public CommonResult<List<ClassListVo>> ClassList()
    {
        return _studyService.ClassList();
    }

    /// <summary>
    ///     年级下学科
    /// </summary>
    /// <param name="cId">年级Id</param>
    /// <returns></returns>
    [HttpGet("SubjectList")]
    public CommonResult<List<SubjectListVo>> SubjectList([FromQuery] long cId)
    {
        return _studyService.SubjectList(cId);
    }

    /// <summary>
    ///     学科的出版社
    /// </summary>
    /// <param name="sId">学科Id</param>
    /// <returns></returns>
    [HttpGet("PressList")]
    public CommonResult<List<PressListVo>> PressList([FromQuery] long sId)
    {
        return _studyService.PressList(sId);
    }

    /// <summary>
    ///     出版社的章节
    /// </summary>
    /// <param name="pId">学科出版社Id</param>
    /// <returns></returns>
    [HttpGet("SectionList")]
    public CommonResult<List<SectionListVo>> SectionList([FromQuery] long pId)
    {
        return _studyService.SectionList(pId);
    }

    /// <summary>
    ///     章节课程
    /// </summary>
    /// <param name="sId">章节Id</param>
    /// <returns></returns>
    [HttpGet("CourseList")]
    public CommonResult<List<CourseListVo>> CourseList([FromQuery] long sId)
    {
        return _studyService.CourseList(_userContext.Id, sId);
    }

    /// <summary>
    ///     课程视频
    /// </summary>
    /// <param name="cId">课程Id</param>
    /// <returns></returns>
    [HttpGet("StudyVideo")]
    public CommonResult<StudyVideoVo> StudyVideo([FromQuery] long cId)
    {
        var result = _studyService.StudyVideo(_userContext.Id, cId);
        if (result.Item1 == null)
            return Problem(result.Item2);
        return result.Item1;
    }

    /// <summary>
    ///     播放进程记录 （客户端5秒调用一次）
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("AddPlayProcess")]
    public CommonResult AddPlayProcess(AddPlayProcessDto dto)
    {
        _studyService.AddPlayProcess(_userContext.Id, dto);
        return Success();
    }
}